package com.ydgg.ltar.api;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ydgg.ltar.model.Member;
import com.ydgg.ltar.service.MemberService;


/**
 * 获取
 * 作者： 张叶
 * 创建时间：2019年3月23日  上午10:18:03
 */
@Controller
@RequestMapping("api/weChat")
public class WeChatApi {
	
	@Autowired
	private MemberService memberService;

	@RequestMapping("/abc")
    public void login(HttpServletResponse response) {
                //这里是回调的url   0e3abbf1f9b9289562d970e4adee7829
				try {
					String redirect_url = URLEncoder.encode("http://ltar.zhyell.com", "UTF-8");
	                String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
	                        "appid=APPID" +
	                        "&redirect_uri=REDIRECT_URI" +
	                        "&response_type=code" +
	                        "&scope=SCOPE" +
	                        "&state=123#wechat_redirect";
	                response.sendRedirect(url.replace("APPID","wx777e45fdfdcfca5b").replace("REDIRECT_URL",redirect_url).replace("SCOPE","snsapi_userinfo"));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    }


	/**
	 * 判断用户
	 * 作者： 张叶
	 * 创建时间：2019年3月23日  上午10:51:29
	 */
	@ResponseBody
	@RequestMapping(value = "/getOpenId")
	public ApiResult getCode(String openId) {
		ApiResult result = new ApiResult();
		if(openId != null && !"".equals(openId)){
			String where = " and status != 'deleted' and (telphone = '" + openId + "' or wechat = '" + openId + "')";
			List<Member> list = memberService.selectByWhere(where);
			if(list.size() == 0){
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无数据";
			}
		}else{
			result.msg.status = 3;
			result.msg.desc = "参数不能为空";
		}
		return result;
	}
	
}
